
<template>
  <span class="ecp-dialog-btn">
    <el-button :disabled="disabled" :type="parentId==1?'primary':'text'" :size="parentId==1?'mini':''" @click="visabled = true">添加子菜单</el-button>
    <el-dialog :title="`添加${menuType=='web'?'管理端':'供应端'}菜单`" :visible.sync="visabled" width="420px" top="100px" center>
      <el-form :model="dialogForm" ref="dialogForm" label-width="90px" class="demo-ruleForm">
        <el-form-item label="分类名称" prop="name" :rules="[
            { required: true, message: '请输入菜单名'}
          ]">
          <el-input type="text" v-model="dialogForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="菜单类型" prop="type" :rules="[
            { required: true, message: '请选择菜单类型'}]">
          <el-select v-model="dialogForm.type" style="width:100%;" placeholder="请选择菜单类型">
            <el-option label="菜单" value="MENU"></el-option>
            <el-option label="按钮" value="BUTTON"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单编码" prop="code" :rules="[{ required: true, message: '请输入菜单编码'}]">
          <el-input type="text" v-model.number="dialogForm.code" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="前端路径" prop="htmlUrl" :rules="[
            { required: true, message: '请输入前端路径'}]">
          <el-input type="text" v-model.number="dialogForm.htmlUrl" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="orderIndex" :rules="[
            { required: true, message: '请输入排序'},
            { type: 'number', message: '排序必须为数字值'}
          ]">
          <el-input type="text" v-model.number="dialogForm.orderIndex" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm" size="mini">取 消</el-button>
        <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </span>
</template>

<script>
import { addWebMenu, addSupplyMenu } from "~/api/";
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    menuType: {
      type: String,
      require: true
    },
    parentId: {
      type: Number,
      default: 1
    }
  },
  data() {
    return {
      visabled: false,
      dialogForm: {
        name: '',
        type: '',
        code: '',
        htmlUrl: '',
        orderIndex: 1,
        requestUrls: []
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs['dialogForm'].validate(async (valid) => {
        if (!valid) return
        const {name, type, code, htmlUrl, orderIndex, requestUrls} = this.dialogForm
        let res
        if (this.menuType == 'web') {
          res = await addWebMenu(this, this.parentId, name, type, code, htmlUrl, orderIndex, requestUrls)
        } else {
          res = await addSupplyMenu(this, this.parentId, name, type, code, htmlUrl, orderIndex, requestUrls)
        }
        console.log(res)
        if (res.code !== '0') return this.$alert(res.message, '提示', {type: 'error'})
        // 成功处理
        this.$message.success('添加成功')
        this.$emit('on-success', {
          name, type, code, htmlUrl, orderIndex, parentId: this.parentId, id: 12
        })
        this.visabled = false
      })
    },
    resetForm() {
      this.$refs['dialogForm'].resetFields()
      this.visabled = false
    }
  }
}
</script>

<style lang="scss" scoped>
.ecp-dialog-btn {
  & /deep/ {
    .el-dialog--center .el-dialog__body {
      padding: 15px;
    }
  }
}
</style>
